package me.firstapp.weixin.controller.index;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.utils.SystemConfig;
import me.firstapp.module.weixin.product.Product;
import me.firstapp.module.weixin.product.ProductCategory;
import me.firstapp.module.weixin.system.Banner;
import me.firstapp.module.weixin.system.Broadcast;
import me.firstapp.service.weixin.product.ProductCategoryService;
import me.firstapp.service.weixin.product.ProductService;
import me.firstapp.service.weixin.system.BannerService;
import me.firstapp.service.weixin.system.BroadcastService;
import me.firstapp.weixin.controller.base.BaseController;

@Controller
public class IndexController extends BaseController {

	@Autowired
	private BannerService bannerService;

	@Autowired
	private ProductCategoryService productCategoryService;

	@Autowired
	private ProductService productService;

	@Autowired
	private BroadcastService broadcastService;

	@RequestMapping("/page/index.htm")
	public ModelAndView indexView(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		getCurrentLoginOpenId(request, response);
		// Banner
		List<Banner> banners = bannerService.findByTag("INDEX");
		JSONArray bannersArray = new JSONArray();
		for (Banner banner : banners) {
			bannersArray.add(JSONObject.toJSON(banner));
		}
		modelMap.put("ATTR1", bannersArray.toJSONString());

		// 分类
		JSONArray categoriesArray = new JSONArray();
		List<ProductCategory> categories = productCategoryService.findProductCategorys();
		for (ProductCategory productCategory : categories) {
			String url = SystemConfig.mServerUrl + "/page/productList.htm?tag=" + productCategory.getTag();
			productCategory.setUrl(url);
			categoriesArray.add(JSONObject.toJSON(productCategory));
		}
		modelMap.put("ATTR2", categoriesArray.toJSONString());

		// 推荐
		JSONArray tuijiansArray = new JSONArray();
		List<Product> tuijians = productService.findByTuiJian();
		for (Product product : tuijians) {
			tuijiansArray.add(JSONObject.toJSON(product));
		}
		modelMap.put("ATTR3", tuijiansArray.toJSONString());

		// 广播
		List<Broadcast> broadcasts = broadcastService.findBroadcasts();
		if (!broadcasts.isEmpty()) {
			JSONArray broadcastsArray = new JSONArray();
			for (Broadcast broadcast : broadcasts) {
				broadcastsArray.add(JSONObject.toJSON(broadcast));
			}
			modelMap.put("ATTR4", broadcastsArray.toJSONString());
		}

		// 活动
		List<Banner> activitys = bannerService.findByTag("ACTIVITY");
		JSONArray activitysArray = new JSONArray();
		for (Banner activity : activitys) {
			activitysArray.add(JSONObject.toJSON(activity));
		}
		modelMap.put("ATTR5", activitysArray.toJSONString());

		return new ModelAndView("index/index", modelMap);
	}
}
